#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "UsePins.h"
#include "Fan.h"

void Fan_Init()
{
}

void Fan_PWM_Init()
{
    
    GPIO_InitTypeDef initStruct;
    TIM_TimeBaseInitTypeDef timBaseDef;
    TIM_OCInitTypeDef ocTypeDef;
    GPIO_InitTypeDef initStructureGreen;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    initStructureGreen.GPIO_Pin = Fan_Pin;
    initStructureGreen.GPIO_Speed = GPIO_Speed_50MHz;
    initStructureGreen.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOB, &initStructureGreen);

    timBaseDef.TIM_Period = 100;
    timBaseDef.TIM_Prescaler = 72000;
    timBaseDef.TIM_CounterMode = TIM_CounterMode_Up;
    timBaseDef.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInit(TIM4, &timBaseDef);

    ocTypeDef.TIM_OCMode = TIM_OCMode_PWM1;
    ocTypeDef.TIM_OCNPolarity = TIM_OCPolarity_High;
    ocTypeDef.TIM_OutputState = TIM_OutputState_Enable;
    ocTypeDef.TIM_Pulse = 60;

    TIM_OC3Init(TIM4, &ocTypeDef);
    TIM_Cmd(TIM4, ENABLE);

}

void Fan_PWM_SetCompare(uint16_t compare)
{
    TIM_SetCompare3(TIM4, compare);
}